import { DataProvider } from "../../../../../fl/data/DataProvider";
import { SAchievementCategroy } from "../../../../../Message/Game/SAchievementCategroy";
import { SAchievementType } from "../../../../../Message/Game/SAchievementType";
import { AchievementConfig } from "../../../resource/AchievementConfig";
import { AchievementMinorData } from "./AchievementMinorData";
import { TAchievementDetatil } from "../../../../../Message/Db/Tables/TAchievementDetatil";
import { SAchievement } from "../../../../../Message/Game/SAchievement";
import { EAchieveStatus } from "../../../../../Message/Game/EAchieveStatus";
import { AchievementMajorData } from "./AchievementMajorData";
import { AchievementBonusData } from "./AchievementBonusData";
import { EAcieveCategory } from "../../../../../Message/Game/EAcieveCategory";
import { Cache } from "../../../cache/Cache";
type int = number;
//class AchievementDataProviderParser
    
    export  class AchievementDataProviderParser
    {
        

        public parse(arg1: Array<any>): DataProvider
        {
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            if (arg1 == null) 
            {
                return this.playerNoGuildDefaultData();
            }
            var loc1=new DataProvider();
            var loc2=this.sortCategory;
            var loc3=0;
            while (arg1 && loc3 < arg1.length) 
            {
                if (typeof arg1[loc3] === "sachievementcategroy") 
                {
                    loc2 = this.sortCategory;
                    loc4 = this.getMajorData(arg1[loc3] as SAchievementCategroy);
                    if (loc4 != null) 
                    {
                        loc1.addItem(loc4);
                    }
                }
                else if (typeof arg1[loc3] === "sachievementtype") 
                {
                    loc2 = this.sortAchievment;
                    loc5 = arg1[loc3] as SAchievementType;
                    loc6 = this.getCurAchievement(loc5);
                    if ((loc7 = AchievementConfig.instance.getAchievementDetail(loc5.category, loc5.type, loc6.code)) != null) 
                    {
                        loc4 = this.parseMinorData(loc5, loc7, loc6);
                        if (loc4 != null) 
                        {
                            loc1.addItem(loc4);
                        }
                    }
                }
                else if (loc4 != null) 
                {
                    loc1.addItem(loc4);
                }
                ++loc3;
            }
            loc1.sort(loc2);
            return loc1;
        }

        public parseForFinishedDatas(arg1: AchievementMinorData): Array<any>
        {
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc1=[];
            var loc2=AchievementConfig.instance.getMinorAchievementDetailThatFinished(arg1);
            var loc3=0;
            while (loc3 < loc2.length) 
            {
                if (!((loc4 = loc2[loc3] as TAchievementDetatil).idx > arg1.achievementLevel)) 
                {
                    if (!(loc4.idx == arg1.achievementLevel && !arg1.isFinished && !arg1.isEnd)) 
                    {
                        (loc5 = new SAchievementType()).end = false;
                        loc5.type = loc4.type;
                        loc5.category = loc4.category;
                        loc5.curAchievements = new SAchievement();
                        loc5.curAchievements.status = EAchieveStatus._EAchieveStatusHadEnd;
                        loc5.curAchievements.curProcess = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
                        loc6 = this.getCurAchievement(loc5);
                        loc1.push(this.parseMinorData(loc5, loc4, loc6));
                    }
                }
                ++loc3;
            }
            loc1.sort(this.sortAchievment);
            return loc1;
        }

        public parseForRecentFinshedByCodes(arg1: Array<any>): DataProvider
        {
            var loc3=0;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc1=new DataProvider();
            var loc2=0;
            while (loc2 < arg1.length) 
            {
                loc3 = int(arg1[loc2]);
                loc4 = AchievementConfig.instance.getAchievementDetailByCode(loc3);
                (loc5 = new SAchievementType()).end = false;
                loc5.type = loc4.type;
                loc5.category = loc4.category;
                loc5.curAchievements = new SAchievement();
                loc5.curAchievements.status = EAchieveStatus._EAchieveStatusHadEnd;
                loc5.curAchievements.curProcess = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
                loc6 = this.getCurAchievement(loc5);
                if ((loc7 = this.parseMinorData(loc5, loc4, loc6)) != null) 
                {
                    loc1.addItem(loc7);
                }
                ++loc2;
            }
            return loc1;
        }

        /* internal  */getMajorData(arg1: SAchievementCategroy): AchievementMajorData
        {
            var loc1=new AchievementMajorData();
            loc1.curAchievement = arg1.currentPoints;
            loc1.categoryId = arg1.category;
            if (arg1.lastIndex != arg1.curIndex) 
            {
                loc1.isLast = false;
            }
            else 
            {
                loc1.isLast = true;
            }
            var loc2=AchievementConfig.instance.getCategoryReward(arg1.category, arg1.curIndex);
            if (loc2 == null) 
            {
                return null;
            }
            loc1.bonusCode = int(loc2.reward.split(",")[1]);
            loc1.curLevel = loc2.idx;
            loc1.index = loc2.idx;
            loc1.name = AchievementConfig.instance.getCategoryName(arg1.category);
            loc1.upLevelAchievement = loc2.process;
            loc1.hasGotBonus = arg1.lastIndex == arg1.curIndex;
            return loc1;
        }

        /* internal  */parseMinorData(arg1: SAchievementType, arg2: TAchievementDetatil, arg3: SAchievement): AchievementMinorData
        {
            var loc1;
            if ((loc1 = AchievementConfig.instance.getAchievementType(arg1.category, arg1.type)) == null) 
            {
                return null;
            }
            var loc2;
            (loc2 = new AchievementMinorData()).category = arg2.category;
            loc2.achievementLevel = arg2.idx;
            var loc3;
            (loc3 = new AchievementBonusData()).achievement = arg2.point;
            loc3.bGold = arg2.bindGold;
            loc3.bCoin = arg2.bindCoin;
            loc3.items = arg2.items;
            loc2.bonusData = loc3;
            loc2.isEnd = arg1.end;
            loc2.code = arg2.code;
            var loc4=int(arg2.goalProcess.split(",")[0]);
            loc2.curValue = int(arg3.curProcess[loc4]);
            loc2.desc = arg2.descStr;
            loc2.iconCode = loc1.icon;
            loc2.type = loc1.type;
            loc2.status = arg1.curAchievements.status;
            loc2.name = loc1.name;
            loc2.totalValue = int(arg2.goalProcess.split(",")[1]);
            if (loc2.isFinished) 
            {
                loc2.curValue = loc2.totalValue;
            }
            return loc2;
        }

        /* internal  */getCurAchievement(arg1: SAchievementType): SAchievement
        {
            return arg1.curAchievements;
        }

        /* internal  */sortCategory(arg1: AchievementMajorData, arg2: AchievementMajorData): int
        {
            if (arg1.isFinished() && !arg1.hasGotBonus) 
            {
                return -1;
            }
            if (arg2.isFinished() && !arg2.hasGotBonus) 
            {
                return 1;
            }
            if (arg1.hasGotBonus && arg1.isLast && !arg2.isLast) 
            {
                return 1;
            }
            if (arg2.isLast && arg2.hasGotBonus) 
            {
                return -1;
            }
            if (arg1.categoryId <= arg2.categoryId) 
            {
                return -1;
            }
            return 1;
        }

        /* internal  */sortAchievment(arg1: AchievementMinorData, arg2: AchievementMinorData): int
        {
            if (arg1.isCanGetBonus()) 
            {
                return -1;
            }
            if (arg2.isCanGetBonus()) 
            {
                return 1;
            }
            if (arg2.isEnd) 
            {
                return -1;
            }
            if (arg1.isEnd) 
            {
                return 1;
            }
            if (arg1.code <= arg2.code) 
            {
                return -1;
            }
            return 1;
        }

        /* internal  */playerNoGuildDefaultData(): DataProvider
        {
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=0;
            var loc9=null;
            var loc10=null;
            var loc11=null;
            var loc1=new DataProvider();
            var loc2=EAcieveCategory._EAchieveCategoryGuild;
            var loc3=[];
            var loc4=AchievementConfig.instance.getAchievementDetails();
            var loc12=0;
            var loc13=loc4;
            for(loc5 of loc13) 
            {
                if (loc5.category != loc2) 
                {
                    continue;
                }
                loc3.push(loc5);
            }
            loc3.sort(this.sortAchievementDetialByType);
            loc6 = [];
            loc7 = 1;
            loc8 = 0;
            while (loc8 < loc3.length) 
            {
                loc6.push(loc3[loc8]);
                loc7 = TAchievementDetatil(loc3[loc8]).type;
                while (loc8 < (loc3.length - 1)) 
                {
                    ++loc8;
                    if (TAchievementDetatil(loc3[loc8]).type == loc7) 
                    {
                        continue;
                    }
                    --loc8;
                    break;
                }
                ++loc8;
            }
            loc8 = 0;
            while (loc8 < loc6.length) 
            {
                loc9 = loc6[loc8] as TAchievementDetatil;
                loc10 = new SAchievementType();
                (loc11 = new SAchievement()).code = loc9.code;
                loc11.curProcess = [0, 0, 0, 0, 0];
                loc11.status = EAchieveStatus._EAchieveStatusNotCompelete;
                loc10.category = loc2;
                loc10.end = false;
                loc10.type = loc9.type;
                loc10.curAchievements = loc11;
                if (!(loc9 == null || loc9.roleLevel > Cache.instance.role.roleInfo.level)) 
                {
                    loc1.addItem(this.parseMinorData(loc10, loc9, loc11));
                }
                ++loc8;
            }
            loc1.sort(this.sortAchievment);
            return loc1;
        }

        /* internal  */copyValue(arg1: Object, arg2: Object): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=arg2;
            for (loc1 in loc3) 
            {
                arg1[loc1] = arg2[loc1];
            }
            return;
        }

        /* internal  */sortAchievementDetialByType(arg1: TAchievementDetatil, arg2: TAchievementDetatil): int
        {
            if (arg1.type < arg2.type) 
            {
                return -1;
            }
            if (arg1.type > arg2.type) 
            {
                return 1;
            }
            if (arg1.code <= arg2.code) 
            {
                return -1;
            }
            return 1;
        }
    }
